 package com.manager.car.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.manager.car.po.ManagerInfo;
import com.manager.car.service.IManagerInfoService;
import com.manager.car.util.Email;
import com.manager.car.util.PageModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @param 登录模板Action类
 * @author xiaoge
 * @Date 2018年11月22日10:54:34
 */
public class ManagerInfoAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private Email email;//发送邮件
	private IManagerInfoService service;//ImanagerInfoService服务
	private PageModel<ManagerInfo> pageModel;
	private ManagerInfo manager; //添加和修改时使用
	private Integer id;    //删除时使用
	private String mangerPwd;
	private String qqEmail;
	private Integer limit;
	private Integer page;
	
	public String querymanagerInfo() {
		
		manager = service.queryManagerInfoById(id);
		
		String str = "";
		if(manager!=null) {
			ActionContext.getContext().put("manager", manager);
			str = "editManager";//通过
		}else {
			str = "error"; //返回登录
		}
		
		return str;
	}
	
	public void updateManagerInfo() throws IOException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		boolean bool = service.updateManager(manager);
		out.print(bool);
		out.flush();
		out.close();
	}
	
	public void addManagerInfo() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		boolean bool = service.addManager(manager);
		out.print(bool);
		out.flush();
		out.close();
	}
	
	public void updateManagerInfoById() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		boolean bool = service.updateManagerInfoStaticById(id);
		out.print(bool);
		out.flush();
		out.close();
	}
	
	public void deleteManagerInfoById() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		boolean bool = service.deleteManagerInfoById(id);
		
		out.print(bool);
		out.flush();
		out.close();
	}
	//查询所有的管理员信息
	public void queryAllManagerInfo() throws IOException {
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if(page==null) {
			page = 1;
		}
		if(limit==null) {
			limit=15;
		}
		PageModel<ManagerInfo> data = service.queryPageModelByHql(page, limit);
		
		Gson gson = new Gson();
		String json = gson.toJson(data);
		
		out.print(json);
		out.flush();
		out.close();
	}
	
	//登录
	public String login() {
		boolean bool = service.getQueryListByHql(manager);
		String str = "";
		if(bool==true) {
			ActionContext.getContext().put("manager", manager);
			str = "pass";//通过
		}else {
			str = "login"; //返回登录
		}
		System.out.println(str);
		return str;
	}
	
	//发送验证码
	@SuppressWarnings("static-access")
	public void sendPwd() throws IOException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		int row = -1;
		try {
			email.SendMsg(mangerPwd, qqEmail);
			row = 1;
		} catch (Exception e) {
			row = 0;
			e.printStackTrace();
		}
		response.getWriter().print(row);
	}

	public Email getEmail() {
		return email;
	}

	public void setEmail(Email email) {
		this.email = email;
	}

	public IManagerInfoService getService() {
		return service;
	}

	public void setService(IManagerInfoService service) {
		this.service = service;
	}

	public PageModel<ManagerInfo> getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel<ManagerInfo> pageModel) {
		this.pageModel = pageModel;
	}

	public ManagerInfo getManager() {
		return manager;
	}

	public void setManager(ManagerInfo manager) {
		this.manager = manager;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getMangerPwd() {
		return mangerPwd;
	}

	public void setMangerPwd(String mangerPwd) {
		this.mangerPwd = mangerPwd;
	}

	public String getQqEmail() {
		return qqEmail;
	}

	public void setQqEmail(String qqEmail) {
		this.qqEmail = qqEmail;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}
	
}
